/* Includes ------------------------------------------------------------------*/
#include "ui.h"
#include "stdio.h"
#include "toast.h"
#include "msg_box.h"
#include "lock.h"

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
lv_obj_t *lobj[LID_MAX];

/* Private function prototypes -----------------------------------------------*/
bool lvgl_font_init(string font_dir, uint8_t font_weight, uint8_t font_style, lv_ft_info_t *ft_info);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : ui_init
 * Description    : init user interface
 * Input          : [ui_t *ui] ui object
 * Output         : None
 * Return         : None
 *******************************************************************************/
void ui_init(ui_t *ui)
{
    // start page init
    ui->scr = NULL;
    ui->keyboard = NULL;
    ui->toast = NULL;
    ui->start.logo = NULL;
    ui->start.img_loading = NULL;
    ui->start.label = NULL;

    // main page init
    {
        // ui->main.scr = NULL;
        { // left box init
            ui->left_box.box = NULL;
            ui->left_box.box_w = UI_LEFT_BOX_WIDTH;
            ui->left_box.box_h = UI_LEFT_BOX_HEIGHT;
            ui->left_box.btn_w = UI_LEFT_BOX_WIDTH;

            ui->main_box.box_w = UI_MAIN_BOX_WIDTH;
            ui->main_box.box_h = UI_MAIN_BOX_HEIGHT;
            ui->main_box.box = NULL;
            // ui->main_box.page_main = NULL;
            // ui->main_box.page_program = NULL;
            // ui->main_box.page_device = NULL;
            // ui->main_box.page_vacuum = NULL;
            // ui->main_box.page_setting = NULL;

            { // home btn init
                ui->left_box.home.btn = NULL;
                ui->left_box.home.label = NULL;
                ui->left_box.home.icon = NULL;
                ui->left_box.home.select = false;
            }
            { // program btn init
                ui->left_box.program.btn = NULL;
                ui->left_box.program.label = NULL;
                ui->left_box.program.icon = NULL;
                ui->left_box.program.select = false;
            }
            { // device btn init
                ui->left_box.device.btn = NULL;
                ui->left_box.device.label = NULL;
                ui->left_box.device.icon = NULL;
                ui->left_box.device.select = false;
            }
            { // vacuum btn init
                ui->left_box.vacuum.btn = NULL;
                ui->left_box.vacuum.label = NULL;
                ui->left_box.vacuum.icon = NULL;
                ui->left_box.vacuum.select = false;
            }
            { // setting btn init
                ui->left_box.setting.btn = NULL;
                ui->left_box.setting.label = NULL;
                ui->left_box.setting.icon = NULL;
                ui->left_box.setting.select = false;
            }
        }
        status_bar_init(&(ui->status_bar));
    }

    lvgl_font_init("font/lvgl_ascii_cn_ft.ttf", 16, FT_FONT_STYLE_NORMAL, &(ui->fnt.size_16));
    lvgl_font_init("font/lvgl_ascii_cn_ft.ttf", 18, FT_FONT_STYLE_NORMAL, &(ui->fnt.size_18));
    lvgl_font_init("font/lvgl_ascii_cn_ft.ttf", 20, FT_FONT_STYLE_NORMAL, &(ui->fnt.size_20));
    lvgl_font_init("font/lvgl_ascii_cn_ft.ttf", 24, FT_FONT_STYLE_NORMAL, &(ui->fnt.size_24));
    lvgl_font_init("font/lvgl_ascii_cn_ft.ttf", 36, FT_FONT_STYLE_NORMAL, &(ui->fnt.size_36));

    toast_init(&(ui->toast), ui->fnt.size_16.font);
    msgbox_font_init(ui->fnt.size_20.font, ui->fnt.size_18.font);
    lock_box_font_init(ui->fnt.size_20.font, ui->fnt.size_18.font);
    // product_name_box_font_init(ui->fnt.size_20.font, ui->fnt.size_18.font);
    for (int i = 0; i < LID_MAX; i++)
    {
        lobj[i] = NULL;
    }
}

/******************************************************************************
 * Function Name  : lvgl_font_init
 * Description    : lvgl font init
 * Input          : font_dir, font_weight, font_style, ft_info
 * Output         : None
 * Return         : true or false, true if the font init success, false if not
 * Notes          : font_dir: font directory
 *                  font_weight: font weight
 *                  font_style: font style
 *                  ft_info: font info
 *******************************************************************************/
bool lvgl_font_init(string font_dir, uint8_t font_weight, uint8_t font_style, lv_ft_info_t *ft_info)
{
    char ft_dir[256];
    memset(ft_dir, 0, sizeof(ft_dir));

    // font path must be relative to LV_FS_STDIO_PATH
    // because lvgl freetype use fopen to open font file
#if defined(LV_USE_FS_STDIO)
    if (font_dir[0] == '/')
        font_dir++;
    sprintf(ft_dir, "%s%s", LV_FS_STDIO_PATH, font_dir);
#else
    sprintf(ft_dir, "%s", font_dir);
#endif
    // printf("ft_dir = %s\n", ft_dir);
    // printf("font_weight = %d\n", font_weight);
    // printf("font_style = %d\n", font_style);

    ft_info->name = ft_dir;
    // printf("ft_info->name = %s\n", ft_info->name);
    ft_info->weight = font_weight;
    ft_info->style = font_style;
    ft_info->mem = NULL;
    if (!lv_ft_font_init(ft_info))
        return false;
    return true;
}

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
